<?php
/**
 * Sistem Informasi Manajerial PT. Aventrurine Prana Asia
 *
 * Class Controller untuk Aktivitas Genset
 *
 * @category Application
 * @package Application_Controller
 * @filesource Application/Controller/AktivitasgensetController.php
 * @author Bayu Santoso
 * @copyright 2012 Gede Technology IT Consulting
 *
 */

class AktivitasgensetController extends Zend_Controller_Action
{

    /**
     * Variabel untuk Model Aktivitas Harian
     *  
     * @var null 
     *
     */
    protected $_aktivitasHarian = null;

    /**
     * Variabel untuk Model Hasil Pemeriksaan
     *
     * @var null 
     *
     */
    protected $_hasilPemeriksaan = null;

    /**
     * Variabel untuk Model Detail Silinder
     *
     * @var null 
     *
     */
    protected $_detailSilinder = null;

    /**
     * Variabel untuk Model Suhu Silinder
     *
     * @var null 
     *
     */
    protected $_suhuSilinder = null;

    /**
     * Variabel untuk Model Detail Stack
     *
     * @var null 
     *
     */
    protected $_detailStack = null;

    /**
     * Variabel untuk Model Suhu Stack
     *
     * @var null 
     *
     */
    protected $_suhuStack = null;

    /**
     * Variabel untuk Model Genset
     *
     * @var null 
     *
     */
    protected $_genset = null;

    public function init()
    {
        $this->_aktivitasHarian = new Application_Model_Aktivitasharian();
        $this->_hasilPemeriksaan = new Application_Model_HasilPemeriksaan();
        $this->_suhuSilinder = new Application_Model_SuhuSilinder();
        $this->_detailSilinder = new Application_Model_DetailSilinder();
        $this->_suhuStack = new Application_Model_SuhuStack();
        $this->_detailStack = new Application_Model_DetailStack();
        $this->_genset = new Application_Model_SpekGenset();
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
        $genset = new Application_Model_Genset();
        $data = $genset->getDataTerpakai();
        
        $this->view->genset = $data;
    }

    public function laporAction()
    {
        
        
        if($this->_getParam('id') == null)
        {
            $this->_redirect('/aktivitasgenset/');
        } else {
            $form = new Application_Form_AktivitasGensetForm();

            $form->getSubForm('gensetSummary')->removeDecorator('form');
            $form->getSubForm('gensetSummary')->removeDecorator('htmlTag');

            $form->getSubForm('keadaanGenset')->removeDecorator('form');
            $form->getSubForm('keadaanGenset')->removeDecorator('htmlTag');

            $form->getSubForm('pemeriksaanUmum')->removeDecorator('form');
            $form->getSubForm('pemeriksaanUmum')->removeDecorator('htmlTag');

            $formGensetSummary = $form->getSubForm('gensetSummary');
            $formKeadaanGenset = $form->getSubForm('keadaanGenset');
            $formPemeriksaanUmum = $form->getSubForm('pemeriksaanUmum');
            $formSuhuKeluaran = $form->getSubForm('suhuKeluaran');
            
            $id = $this->_getParam('id');
            
            $gensetModel = new Application_Model_Genset();
            $result = $gensetModel->getDataJoinId($id);            
            
            $no_silinder = $result['no_silinder'];
            $no_stack = $result['no_stack'];
            
            $silinder = 1;
            while($no_silinder!=0){
                $formSuhuKeluaran->addElement('text', 'suhu_silinder_'.$silinder,array('label' => 'Silinder Ke '.$silinder,'required' => true));
                $no_silinder--;
                $silinder++;
            }
            
            $stack = 1;
            while($no_stack!=0){
                $formSuhuKeluaran->addElement('text', 'suhu_stack_'.$stack,array('label' => 'Stack Ke '.$stack, 'required' => true));
                $no_stack--;
                $stack++;
            }
            
            $formSuhuKeluaran->removeDecorator('form');
            $formSuhuKeluaran->removeDecorator('htmlTag');
            
            $this->view->suhuSilinder = $formSuhuKeluaran;
            $this->view->gensetSummary = $formGensetSummary;
            $this->view->keadaanGenset = $formKeadaanGenset;
            $this->view->pemeriksaanUmum = $formPemeriksaanUmum;
            
            $this->view->submit = $form->getElement('submit');
            $this->view->batal = $form->getElement('batal');
            
            if($this->_request->isPost())
            {
                if($form->isValid($this->_request->getPost()))
                {
                    
                    /*Insert Into Tabel Hasil Pemeriksaan*/
                    $data = array(
                        'kw' => $formPemeriksaanUmum->getValue('kw'),
                        'fuel_press' => $formPemeriksaanUmum->getValue('fuel_press'),
                        'oil_press' => $formPemeriksaanUmum->getValue('oil_press'),
                        'jw_temp' => $formPemeriksaanUmum->getValue('jw_temp'),
                        'oil_temp' => $formPemeriksaanUmum->getValue('oil_temp'),
                        'fuel_meter' => $formPemeriksaanUmum->getValue('fuel_meter'),
                        'cos_q' => $formPemeriksaanUmum->getValue('cos_q'),
                        'kondisi' => $formPemeriksaanUmum->getValue('kondisi'),
                        'deskripsi' => $formPemeriksaanUmum->getValue('deskripsi'),
                        'id_status' => $formGensetSummary->getValue('id_status')
                    );
                    
                    $id_hasil_pemeriksaan = $this->_hasilPemeriksaan->hasilPemeriksaanBaru($data);
                    
                    /*Insert Into Tabel Suhu Silinder, Tabel Detail Silinder and Tabel Suhu Stack, Tabel Detail Stack*/
                    
                    $suhu = $formSuhuKeluaran->getValues();
                    
                    $no_silinder = $result['no_silinder'];
                    $no_stack = $result['no_stack'];
                    
                    $silinder = 1;
                    $stack = 1;
                    
                    while($no_silinder!=0){
                        $data = array(
                             'suhu_silinder' => $suhu['suhu_silinder_'.$silinder],
                             'no_silinder' => $silinder
                        );
                        
                        $id_suhu_silinder = $this->_suhuSilinder->suhuSilinderBaru($data);
                        
                        $data = array(
                            'id_hasil_pemeriksaan' => $id_hasil_pemeriksaan,
                            'id_suhu_silinder' => $id_suhu_silinder
                        );
                        
                        $this->_detailSilinder->detailSilinderBaru($data);
                        
                        $silinder++;
                        $no_silinder--;
                    }
                    
                    while($no_stack!=0){
                        $data = array(
                            'suhu_stack' => $suhu['suhu_stack_'.$stack],
                            'no_stack' => $stack
                        );
                        
                        $id_suhu_stack = $this->_suhuStack->suhuStackBaru($data);
                        
                        $data = array(
                            'id_hasil_pemeriksaan' => $id_hasil_pemeriksaan,
                            'id_suhu_stack' => $id_suhu_stack
                        );
                        
                        $this->_detailStack->detailStackBaru($data);
                        
                        $stack++;
                        $no_stack--;
                    }
                    
                    /*Insert Into Tabel Aktivitas Harian*/
                    
                    $auth = Zend_Auth::getInstance();
                    $id_user =  $auth->getStorage()->read()->id_user;
                    
                    $data = array(
                        'tanggal_aktivitas' => date('Y-m-d'),
                        'id_waktu_pemeriksaan' => $formGensetSummary->getValue('id_waktu_pemeriksaan'),
                        'id_genset' => $result['id_genset'],
                        'id_hasil_pemeriksaan' => $id_hasil_pemeriksaan,
                        'id_user' => $id_user
                    );
                    
                    $this->_aktivitasHarian->aktivitasBaru($data);
                    /*Update to Tabel Spek Engine*/
                    
                    $data = array(
                        'jam_operasi' => $formGensetSummary->getValue('jam_operasi')
                    );
                    
                    $this->_genset->spekGensetUpdate($data, $result['id_engine_genset']);
                    
                    $this->_redirect('aktivitasgenset');
                    
                } else {
                    $formGensetSummary->populate($this->_request->getPost());
                    $formGensetSummary->populate(array('serial_number_genset' => $result['serial_number_genset']));
                    $formKeadaanGenset->populate($this->_request->getPost());
                    $formPemeriksaanUmum->populate($this->_request->getPost());
                    $formSuhuKeluaran->populate($this->_request->getPost());
                }
            } else {
                $formGensetSummary->populate($result);
            }
        }
        
    }

    public function editAction()
    {
        
    }

    public function viewAction()
    {
        
    }


}

